var helper = {

  titleName: "我的图书",

  //统计购物车数量
  cartCount: function(that, allDatas) {

    //获取当前购物车中的所购买的产品的总数量

    var totalCounts = 0;

    //购买的图书的总数量
    if (allDatas.book.length > 0) {

      for (let i = 0; i < allDatas.book.length; i++) {

        totalCounts += parseInt(allDatas.book[i].count)

      }

    }

    //购买的音乐的总数量
    if (allDatas.music.length > 0) {

      for (let i = 0; i < allDatas.music.length; i++) {

        totalCounts += parseInt(allDatas.music[i].count)

      }

    }


    //购买的电影的总数量
    if (allDatas.movie.length > 0) {

      for (let i = 0; i < allDatas.movie.length; i++) {

        totalCounts += parseInt(allDatas.movie[i].count)

      }

    }

    //显示购物车数量
    that.setData({
      cartCounts: totalCounts
    })

    return totalCounts;
  },

  addsub: function(e, openid, that_, action) {
    //所点分类
    let item = e.currentTarget.dataset.item;

    //所点产品id
    let pid = e.currentTarget.dataset.pid;

    //每点击一次，1.重新更新Storage 2.重新渲染数据

    // 1.获取本地数据
    var storageDatas = wx.getStorageSync(openid);

    //2.更新本地数据
    var bookData = storageDatas.book;

    var musicData = storageDatas.music;

    var movieData = storageDatas.movie;

    var this_ = this;

    //是删除还是增加
    if (item == 'book') {

      for (let i = 0; i < bookData.length; i++) {

        if (bookData[i].pid == pid) {

          if (action == 'add') {

            bookData[i].count++;

          } else if (action == 'sub') {

            if (bookData[i].count == 1) {
              wx.showModal({
                title: '提示',
                content: '确定删除吗？',
                success(res) {

                  if (res.confirm) {
                    //this_:小助手
                    // 递归调用
                    this_.addsub(e, openid, that_, 'del');
                  } else if (res.cancel) {
                    console.log('用户取消')
                  }

                }
              })

            } else {
              bookData[i].count--;
            }
          } else if (action == 'del') {

            bookData.splice(i, 1);

          }

        }
      }
    } else if (item == 'music') {

      for (let i = 0; i < musicData.length; i++) {

        if (musicData[i].pid == pid) {

          if (action == 'add') {

            musicData[i].count++;

          } else if (action == 'sub') {

            if (musicData[i].count == 1) {
              wx.showModal({
                title: '提示',
                content: '确定删除吗？',
                success(res) {

                  if (res.confirm) {
                    //this_:小助手
                    // 递归调用
                    this_.addsub(e, openid, that_, 'del');
                  } else if (res.cancel) {
                    console.log('用户取消')
                  }
                }
              })

            } else {

              musicData[i].count--;

            }
          } else if (action == 'del') {

            musicData.splice(i, 1);

          }
        }
      }
    } else {

      for (let i = 0; i < movieData.length; i++) {

        if (movieData[i].pid == pid) {

          if (action == 'add') {

            movieData[i].count++;

          } else if (action == 'sub') {

            if (movieData[i].count == 1) {
              wx.showModal({
                title: '提示',
                content: '确定删除吗？',
                success(res) {

                  if (res.confirm) {
                    //this_:小助手
                    // 递归调用
                    this_.addsub(e, openid, that_, 'del');
                  } else if (res.cancel) {
                    console.log('用户取消')
                  }

                }
              })

            } else {

              movieData[i].count--;

            }

          } else if (action == 'del') {

            movieData.splice(i, 1);

          }
        }
      }
    }

    //1.3重新设置storage
    wx.setStorageSync(openid, storageDatas)

    //2.重新渲染到模板
    that_.setData({
      result: '',
      bookDatas: storageDatas.book,
      musicDatas: storageDatas.music,
      movieDatas: storageDatas.movie
    })

    // 3.购物车数量显示
    let counts = this.cartCount(that_, storageDatas);

    wx.setTabBarBadge({
      index: 2,
      text: counts.toString(),
    })

    //4.统计总价
    let totalPrice = this.countPrice(storageDatas);

    //渲染到模板
    that_.setData({
      totalPrice: totalPrice
    })
  },

  // 统计购物车总金额
  countPrice:function(allDatas){

    let totalPrice=0;

    for (var i in allDatas) {
      allDatas[i].forEach(function (item) {
        let counts = item.count;
        // 单价
        let price = item.pdatas.price;
        //总计
        totalPrice += counts * price;
      })
    }
    return totalPrice.toFixed(2);
  }
}

module.exports = helper;